public class MyStack {
    /*创建链表的节点*/
    static class Node{
        public int val;
        public Node next;

        public Node(int val){
            this.val = val;
        }
    }
    public Node head;
    public int size;

    //实现入栈方法
    public void push(int val){
        Node node = new Node(val);
        /*如果此时链表中没有节点，那么将node设为head，并将有效元素个数size加1
        * 如果链表中已经有元素的话，将node节点头插到head节点前，并设为head*/
        if (size == 0){
            head = node;
        }else {
            node.next = head;
            head = node;
        }
        size++;
    }
    //实现出栈方法
    /*返回当前头节点的值，并将头节点后移*/
    public int pop(){
        int h = head.val;
        head = head.next;
        size--;
        return h;
    }
    //获取栈顶元素的peek方法
    public int peek(){
        return head.val;
    }
    //判断栈是否为空
    public boolean empty(){
        return size == 0;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(12);
        stack.push(24);
        stack.push(26);
        stack.push(58);
        int b = stack.pop();
        int a = stack.peek();
        System.out.println(b);
        System.out.println(a);

    }
}

